package com.tianlu.store.controller;


import com.tianlu.store.common.NoCheckToken;
import com.tianlu.store.common.ResponseResult;
import com.tianlu.store.utils.CosFileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * 文件管理接口层
 *
 * @author 梁东烂
 * @since 2022-07-04 10:28:28
 */
@Api(tags = "文件管理Rest接口")
@RestController
@RequestMapping("/api/cosFile")
@Slf4j
public class CosFileRestController {
    /**
     * 上传
     *
     * @param file 文件对象
     * @return ResponseResult<String>
     * @author liangdl
     * @since 2022/7/21 14:13
     */
    @ApiOperation("上传文件")
    @PostMapping(value = "/upload")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件")
    })
    @NoCheckToken
    public Callable<ResponseResult<String>> upload(@RequestParam("file") MultipartFile[] file) {
        String url = CosFileUtils.batchUpload(file);
        return () -> ResponseResult.success(url);
    }

    /**
     * 下载
     *
     * @param url 文件下载地址
     * @return ResponseResult<String>
     * @author liangdl
     * @since 2022/7/21 14:13
     */
    @ApiOperation("下载文件")
    @PostMapping(value = "/download")
    public ResponseResult<String> download(@RequestParam String url, @RequestParam(required = false) Long expirationTime) {
        String download = CosFileUtils.download(url, expirationTime);
        return ResponseResult.success(download);
    }

    /**
     * 批量下载
     *
     * @param urlList 地址
     * @return ResponseResult<List < String>>
     * @author liangdl
     * @since 2022/7/21 14:13
     */
    @ApiOperation("批量下载文件")
    @PostMapping(value = "/batchDownload")
    public ResponseResult<Boolean> batchDownload(HttpServletResponse response, @RequestParam List<String> urlList) {
        CosFileUtils.batchDownload(response, urlList, null);
        return ResponseResult.success(true);
    }

}

